package cn.gson.crm.model.service;

import cn.gson.crm.model.dao.WorkDao;
import cn.gson.crm.model.mapper.CenterMapper;
import cn.gson.crm.model.mapper.WorkMapper;
import cn.gson.crm.model.pojos.Employee;
import cn.gson.crm.model.pojos.Role;
import cn.gson.crm.model.pojos.User;
import cn.gson.crm.model.pojos.Work;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class WorkService {
    @Autowired
    WorkMapper workMapper;
    @Autowired
    WorkDao workDao;
    @Autowired
    CenterMapper centerMapper;
    //查询所有
    public List<Work> select1(String min){
        return workMapper.select1(min);
    }
    //新增
    public void addWork(Work work){
        workDao.save(work);
    }
    //查询员工
    public List<Employee> selectEmployee(){return workMapper.selectEmployee();}
    //查询饼状图1
    public Map<String ,Object> selectWork(){
        List<Work> workList=workMapper.selectWork();
        Map<String,Object> map=new HashMap<>();
        List<String> lei=new ArrayList<>();//类型
        List<Integer> shul=new ArrayList<>();//数量
        for(int i=0;i<workList.size();i++){
            lei.add(workList.get(i).getWorkType());
            shul.add(workDao.selectWork(workList.get(i).getWorkType()));
        }
        map.put("lei",lei);
        map.put("shul",shul);
        return map;
    }
    //查询饼状图2
    public Map<String ,Object> selectWork2(){
        List <Employee> employees=workMapper.selectEmployee();
        Map<String,Object> map=new HashMap<>();
        List<String> lei =new ArrayList<>();//负责人
        List<Integer> shul=new ArrayList<>();//数量
        for(int i=0;i<employees.size();i++){
            lei.add(employees.get(i).getYname());
            shul.add(workDao.sellApprover(employees.get(i).getYgId()));
        }
        map.put("lei",lei);
        map.put("shul",shul);
        return map;
    }
    //判断权限
    public Integer fouyuan(HttpSession session){
        User login = (User)session.getAttribute("login");
        int ygId=login.getyId();
        //查询是否为老板
        List<User> userList=centerMapper.selectUserRole(ygId);
        List<Role> roleList=userList.get(0).getRoleList();
        for(int i=0;i<roleList.size();i++){
            if(roleList.get(i).getjZil()==1){
                return 0;
            }
        }
        int ygId2= (int) login.getEmployees().get(0).getYgId();
        return ygId2;
    }
}
